package com.slofzx.nuoda.evaluation.project.service.impl;

import com.slofzx.nuoda.common.util.DataConvertUtil;
import com.slofzx.nuoda.evaluation.project.dao.AnalysisDAO;
import com.slofzx.nuoda.evaluation.project.service.IndexService;
import com.slofzx.nuoda.evaluation.project.util.DoubleUtil;
import com.slofzx.nuoda.evaluation.project.vo.IndexCountVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private AnalysisDAO analysisDAO;
    @Override
    public List<IndexCountVO> queryOverallInfo() {
        final List<IndexCountVO> indexCountVOS = analysisDAO.queryBatchCount();
        if (CollectionUtils.isEmpty(indexCountVOS)) {
            return Collections.EMPTY_LIST;
        }
        final Map<String, IndexCountVO> countVOMap = indexCountVOS.stream().filter(f -> Objects.nonNull(f) && StringUtils.isNotEmpty(f.getType())).collect(Collectors.toMap(IndexCountVO::getType, Function.identity(), (v1, v2) -> v1));
        final List<IndexCountVO> projectData = analysisDAO.queryProjectData();
        if (CollectionUtils.isNotEmpty(projectData)) {
            for (IndexCountVO item : projectData) {
                if (item == null) {
                    continue;
                }
                final IndexCountVO indexCountVO = countVOMap.get(item.getType());
                if (indexCountVO == null) {
                    continue;
                }
                indexCountVO.setProjectCount(item.getProjectCount());
                indexCountVO.setInfWellCount(item.getInfWellCount());
                final Double newCapacity = DataConvertUtil.parse2Double(item.getNewCapacity());
                indexCountVO.setNewCapacity(DoubleUtil.getScale(newCapacity,2));
                final Double totalInvest = DataConvertUtil.parse2Double(item.getConstructionInvest());
                if (totalInvest == 0 || newCapacity == 0) {
                    indexCountVO.setmTProductivityInvest(0D);
                } else {
                    indexCountVO.setmTProductivityInvest(DoubleUtil.getScale(totalInvest / newCapacity / 100, 1));
                }
                indexCountVO.setTotalInvest(totalInvest);
            }
        }
        final List<IndexCountVO> projectCountList = analysisDAO.queryProjectCount();
        if (CollectionUtils.isNotEmpty(projectCountList)) {
            for (IndexCountVO item : projectCountList) {
                final IndexCountVO indexCountVO = countVOMap.get(item.getType());
                if (indexCountVO == null) {
                    continue;
                }
                indexCountVO.setProjectCount(item.getProjectCount());
            }
        }
        return new ArrayList<>(countVOMap.values());
    }
}
